package com.ellecity06.cwhiskymall.ui.invite

import android.os.Bundle
import android.view.View
import androidx.core.text.buildSpannedString
import androidx.core.text.scale
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.ellecity06.common.base.BaseMvvmActivity
import com.ellecity06.common.ktx.launchActivity
import com.ellecity06.common.ktx.observeEvent
import com.ellecity06.cwhiskymall.adapter.MemberInviteListAdapter
import com.ellecity06.cwhiskymall.bean.response.MemberInviteResponse
import com.ellecity06.cwhiskymall.databinding.ActivityInviteFriendBinding
import com.ellecity06.cwhiskymall.storage.UserStorage
import com.ellecity06.cwhiskymall.ui.invite.more.InviteListActivity
import com.ellecity06.cwhiskymall.view.dialog.ShareInviteDialog

/**
 * @author ellecity06
 * @time 2024/11/4 20:59
 * @des 邀请朋友
 */
class InviteFriendActivity :
    BaseMvvmActivity<ActivityInviteFriendBinding, InviteFriendViewModel>(), View.OnClickListener {
    private val adapter: MemberInviteListAdapter by lazy { MemberInviteListAdapter() }
    override fun inflateBinding(): ActivityInviteFriendBinding {
        return ActivityInviteFriendBinding.inflate(layoutInflater)
    }

    private val textRule =
        "1.【如何找新用户?】\n把邀请海报分享给同事或者更多的群，只要好友扫码跳转网页注册，你就可以看到好友是否下单为新用户; \n 2.【活动参与流程?】\n ①发邀请海报给好友>>②)好友(未注册过或注册未下单)扫描邀请海报二 维码注册完成，首单支付成功后并签收>>(③)奖励首单消费金额的 %s 作为贡献金；\n 3.【如何确定邀请关系成立?)\n 好友扫码注册后，好友受邀信息出现在邀请人的“我的邀请”菜单下，为 绑定成功，邀请人可通过邀请菜单自查好友受邀状态; \n 4.【邀请有没有上限?)\n 好友首单消费金额的%s作为贡献金奖励……好友首单消费金额越高，你获得的奖励越多，以此往复循环，奖励没有上限; \n 5.【新人有啥奖励?)\n 被邀新用户扫码注册下单可领198贡献金奖励，连续30天登录首页打卡领取6.6贡献金；\n6.【温馨提示:以下场景算邀请失败)\n ①好友通过非邀请方式完成首单，即视为邀请失败，不发放奖励的非邀 请方式：包括但不限于地推、媒体广告等; ②好友已参与过平台内新人首单活动，则邀请失败; "

    override fun initialize(savedInstanceState: Bundle?) {
        title = "邀请好友"
        initView()
        fetchData()
    }

    override fun initEvent() {
        binding.viewMore.setOnClickListener(this)
        binding.btnSave.setOnClickListener(this)
    }

    private fun initView() {
        binding.rvInvite.layoutManager = LinearLayoutManager(this)
        binding.rvInvite.adapter = adapter
        binding.tvRule.text = String.format(textRule, "10%","10%")
    }

    override fun initObserver() {
        viewModel.state.observeEvent(this) {
            when (it) {
                is InviteFriendState.MarqueeInfo -> {
                    binding.marqueeView.text = it.data
                    binding.marqueeView.start()
                }

                is InviteFriendState.MemberInviteList -> {
                    adapter.submitList(it.data)
                    handlerEmpty(it.data)

                }

                is InviteFriendState.RegisterH5 -> {
                    // 拼接上自己的用户id
                    val registerUrl =
                        "${it.url}/#/Register?activityId=1002&invitationCode=${UserStorage.user?.shardCode}"
                    ShareInviteDialog(this, registerUrl).show()
                }

                is InviteFriendState.OtherSettingValue -> {
                    binding.tvValueP.text = buildSpannedString {
                        append("${it.value}")
                        scale(0.7f) {
                            append("%")
                        }
                    }
                    binding.tvRule.text = String.format(textRule, "${it.value}%", "${it.value}%")


                }

                else -> {}
            }
        }
    }

    private fun handlerEmpty(data: List<MemberInviteResponse.Member>) {
        if (data.isEmpty()) {
            binding.rvInvite.visibility = View.GONE
            binding.line1.visibility = View.GONE
            binding.viewMore.visibility = View.GONE
            binding.tvNoData.visibility = View.VISIBLE
        } else {
            binding.rvInvite.visibility = View.VISIBLE
            binding.line1.visibility = View.VISIBLE
            binding.viewMore.visibility = View.VISIBLE
            binding.tvNoData.visibility = View.GONE
        }
    }

    private fun fetchData() {
        lifecycleScope.launchWhenCreated {
            viewModel.intentChannel.send(InviteFriendIntent.FetchData)
        }
    }

    override fun onClick(view: View?) {
        when (view?.id) {
            binding.viewMore.id -> {
                launchActivity<InviteListActivity>()
            }

            binding.btnSave.id -> {
                lifecycleScope.launchWhenCreated {
                    viewModel.intentChannel.send(InviteFriendIntent.GetRegisterUrl)
                }

            }
        }
    }
}